home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / a-strbou.adb < prev    next >
Text File  |  1994-05-19  |  39KB  |  1,254 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                  A D A . S T R I N G S . B O U N D E D                   --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.4 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. --  Note: This code is derived from the ADAR.CSH public domain Ada 83
  26. --  versions of the Appendix C string handling packages. Major changes
  27. --  have been made from this starting point. Notably, all use of functions
  28. --  returning strings, and of string concatenation in particular, have been
  29. --  avoided, to make absolutely sure that the heap is not used. The data
  30. --  structure has been simplified to avoid the embedded variant record,
  31. --  which makes it much easier to modify the data of a bounded string
  32. --  in place. Also all dependence on Ada.Strings.Fixed has been removed.
  33.  
  34.  
  35. with Ada.Strings.Search;
  36.  
  37. package body Ada.Strings.Bounded is
  38.  
  39.    package body Generic_Bounded_Length is
  40.  
  41.       ---------
  42.       -- "=" --
  43.       ---------
  44.  
  45.       function "=" (Left, Right : in Bounded_String) return Boolean is
  46.       begin
  47.          return Left.Length = Right.Length
  48.            and then Left.Data (1 .. Left.Length) =
  49.                     Right.Data (1 .. Right.Length);
  50.       end "=";
  51.  
  52.       ---------
  53.       -- "<" --
  54.       ---------
  55.  
  56.       function "<" (Left, Right : in Bounded_String) return Boolean is
  57.       begin
  58.          return Left.Data (1 .. Left.Length) < Right.Data (1 .. Right.Length);
  59.       end "<";
  60.  
  61.       ----------
  62.       -- "<=" --
  63.       ----------
  64.  
  65.       function "<=" (Left, Right : in Bounded_String) return Boolean is
  66.       begin
  67.          return Left.Data (1 .. Left.Length) <= Right.Data (1 .. Right.Length);
  68.       end "<=";
  69.  
  70.       ---------
  71.       -- ">" --
  72.       ---------
  73.  
  74.       function ">" (Left, Right : in Bounded_String) return Boolean is
  75.       begin
  76.          return Left.Data (1 .. Left.Length) > Right.Data (1 .. Right.Length);
  77.       end ">";
  78.  
  79.       ----------
  80.       -- ">=" --
  81.       ----------
  82.  
  83.       function ">=" (Left, Right : in Bounded_String) return Boolean is
  84.       begin
  85.          return Left.Data (1 .. Left.Length) >= Right.Data (1 .. Right.Length);
  86.       end ">=";
  87.  
  88.       ---------
  89.       -- "*" --
  90.       ---------
  91.  
  92.       function "*"
  93.         (Left  : in Natural;
  94.          Right : in Character)
  95.          return  Bounded_String
  96.       is
  97.       begin
  98.          return Replicate (Left, Right, Strings.Error);
  99.       end "*";
  100.  
  101.       function "*"
  102.         (Left  : in Natural;
  103.          Right : in String)
  104.          return  Bounded_String
  105.       is
  106.       begin
  107.          return Replicate (Left, Right, Strings.Error);
  108.       end "*";
  109.  
  110.       function "*"
  111.         (Left  : in Natural;
  112.          Right : in Bounded_String)
  113.          return  Bounded_String
  114.       is
  115.       begin
  116.          return Replicate (Left, Right, Strings.Error);
  117.       end "*";
  118.  
  119.       ---------
  120.       -- "&" --
  121.       ---------
  122.  
  123.       function "&" (Left, Right : in Bounded_String)
  124.          return Bounded_String is
  125.       begin
  126.          return Append (Left, Right, Drop => Strings.Error);
  127.       end "&";
  128.  
  129.       function "&" (Left : in Bounded_String; Right : in String)
  130.          return Bounded_String is
  131.       begin
  132.          return Append (Left, Right, Drop => Strings.Error);
  133.       end "&";
  134.  
  135.       function "&" (Left : in String; Right : in Bounded_String)
  136.          return Bounded_String is
  137.       begin
  138.          return Append (Left, Right, Drop => Strings.Error);
  139.       end "&";
  140.  
  141.       function "&" (Left : in Bounded_String; Right : in Character)
  142.          return Bounded_String is
  143.       begin
  144.          return Append (Left, Right, Drop => Strings.Error);
  145.       end "&";
  146.  
  147.       function "&" (Left : in Character; Right : in Bounded_String)
  148.          return Bounded_String is
  149.       begin
  150.          return Append (Left, Right, Drop => Strings.Error);
  151.       end "&";
  152.  
  153.       ------------
  154.       -- Append --
  155.       ------------
  156.  
  157.       --  Case of Bounded_String and Bounded_String
  158.  
  159.       function Append
  160.         (Left, Right : in Bounded_String;
  161.          Drop        : in Strings.Truncation  := Strings.Error)
  162.          return        Bounded_String
  163.       is
  164.          Result : Bounded_String;
  165.          Llen   : constant Length_Range := Left.Length;
  166.          Rlen   : constant Length_Range := Right.Length;
  167.  
  168.       begin
  169.          if Llen + Rlen <= Max_Length then
  170.             Result.Length := Llen + Rlen;
  171.             Result.Data (1 .. Llen) := Left.Data;
  172.             Result.Data (Llen + 1 .. Llen + Rlen) := Right.Data;
  173.  
  174.          else
  175.             Result.Length := Max_Length;
  176.  
  177.             case Drop is
  178.                when Strings.Right =>
  179.                   if Llen >= Max_Length then
  180.                      Result.Data (1 .. Max_Length) :=
  181.                        Left.Data (1 .. Max_Length);
  182.  
  183.                   else
  184.                      Result.Data (1 .. Llen) := Left.Data;
  185.                      Result.Data (Llen + 1 .. Max_Length) :=
  186.                        Right.Data (1 .. Max_Length - Llen);
  187.                   end if;
  188.  
  189.                when Strings.Left =>
  190.                   if Rlen >= Max_Length then
  191.                      Result.Data (1 .. Max_Length) :=
  192.                        Right.Data (Rlen - (Max_Length - 1) .. Rlen);
  193.  
  194.                   else
  195.                      Result.Data (1 .. Max_Length - Rlen) :=
  196.                        Left.Data (Llen - (Max_Length - Rlen + 1) .. Llen);
  197.                      Result.Data (Max_Length - Rlen + 1 .. Max_Length) :=
  198.                        Right.Data;
  199.                   end if;
  200.  
  201.                when Strings.Error =>
  202.                   raise Strings.Length_Error;
  203.             end case;
  204.          end if;
  205.  
  206.          return Result;
  207.       end Append;
  208.  
  209.       --  Case of Bounded_String and String
  210.  
  211.       function Append
  212.         (Left  : in Bounded_String;
  213.          Right : in String;
  214.          Drop  : in Strings.Truncation := Strings.Error)
  215.          return  Bounded_String
  216.       is
  217.          Result : Bounded_String;
  218.          Llen   : constant Length_Range := Left.Length;
  219.          Rlen   : constant Length_Range := Right'Length;
  220.  
  221.       begin
  222.          if Llen + Rlen <= Max_Length then
  223.             Result.Length := Llen + Rlen;
  224.             Result.Data (1 .. Llen) := Left.Data;
  225.             Result.Data (Llen + 1 .. Llen + Rlen) := Right;
  226.  
  227.          else
  228.             Result.Length := Max_Length;
  229.  
  230.             case Drop is
  231.                when Strings.Right =>
  232.                   if Llen >= Max_Length then
  233.                      Result.Data (1 .. Max_Length) :=
  234.                        Left.Data (1 .. Max_Length);
  235.  
  236.                   else
  237.                      Result.Data (1 .. Llen) := Left.Data;
  238.                      Result.Data (Llen + 1 .. Max_Length) :=
  239.